<?php

namespace App\Http\Controllers\Admin;

use App;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Model\Article;
use App\Model\Tag;
use App\Model\ArticleType;
use App\Model\ArticleTag;
use Illuminate\Support\Facades\Cache;


class ArticleController extends Controller
{

    public function index(Request $request)
    {
        $assign = [];
        return view('admin.article.index', $assign);
    }

    //添加
    public function create(Request $request)
    {

        //分类
        $type_list = ArticleType::All();
        $tag_list = Tag::All();
        if (!empty($request->input('id'))) {
            $article = Article::find($request->input('id'));

            $assign = compact('type_list', 'tag_list', 'article');
        } else {
            $assign = compact('type_list', 'tag_list');
        }
        //标签
        return view('admin.article.add', $assign);
    }

    //添加与修改业务
    public function store(App\Http\Requests\StoreArticle $request)
    {
        $param = $request->input();
        unset($param['editormd-image-file']);
        $param['cover'] = $param['cover'] ?? '';
        $param['markdown'] = trim($param['content']);
        $param['content'] = trim($param['bjy-content-html-code']);// markdown_to_html($param['content']); //转成网页格式
        unset($param['bjy-content-html-code']);
        $param['author'] = Auth('admin')->user()->name;
        $tag_ids = $param['tag_ids'];
        $model_tag = '';  //模型中已存在标签
        $del_tag_ids = '';//删除的标签
        unset($param['release']);
        if (empty($param['id'])) {
            $article = new Article();
            $article = $article->create($param);
        } else {
            $article = Article::find($param['id']);
            //获取模型中的tag_ids
            $model_tag = $article->tag_ids;
            //修改
            $article = set_attributes($article, $param);
            $article->save();
        }
        $article_id = $article->id;
        $tag_ids = explode(',', $tag_ids);
        if (!empty($model_tag)) {
            $model_tag = explode(',', $model_tag);
            $model_tag = array_filter($model_tag, function ($item) {
                return !empty($item);
            });
        }
        $tag_ids = array_filter($tag_ids, function ($item) {
            return !empty($item);
        });
        $n = 0;
        if (!empty($model_tag)) {
            ArticleTag::where(['article_id' => $article_id])->delete();
        }
        if (!empty($tag_ids)) {
            foreach ($tag_ids as $key => $value) {
//                $article_tag = ArticleTag::where(['tag_id' => $value], ['article_id' => $article_id])->first();
//                if (empty($article_tag)) {
                $article_tag = new ArticleTag();
                $article_tag->tag_id = $value;
                $article_tag->article_id = $article_id;
                $article_tag->save();
            }
        }
        Cache::flush();
        return respone_return(1, '操作成功');
    }

    //删除
    public function del(Request $request)
    {
        $ids = $request->input('ids');
        $article = new Article();
        return $article->deleteAll($ids);
    }
//    public  function destroy(Request $request){
//        $ids=$request->input('ids');
//        $map=[
//            'id'=>$ids
//        ];
//        if(Article::destroyData($ids)){
//            return respone_return('200','删除成功');
//        }else{
//            return respone_return('0','删除失败');
//        }
//    }
}
